package com.hddz.acquisition;

import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.UUID;

import static android.content.Context.MODE_PRIVATE;

public class GlobalConst {
	public final static int WH_DIALOG = 1000;
	public final static int WH_BC = 1001;
	public final static int WH_LIST = 1002;
	public final static int PRINT_LIST = 1003;
	public final static int NFC_DIALOG = 1004;
	public final static int NFC_HISTROY_DIALOG = 1005;
	public final static int NFC_HISTROY_COLUMN = 1006;
	/**
	 * 图片来源选择
	 */
	public final static int PHOTOS_DIALOG = 1007;
	/**
	 * 用水状态选择
	 */
	public final static int YSZT_VIEW = 1008;
	/**
	 * 拍摄照片
	 */
	public static final int TAKE_PICTURE = 1009;
	/**
	 * 相册返回
	 */
	public static final int LOAD_IMAGE = 1010;
	/**
	 * 裁剪返回
	 */
	public static final int CUT_PHOTO = 1011;


	/**
	 * 图片来源选择
	 */
	public final static int CXTJ_DIALOG = 1012;

	public static final int SELECTIMG_SEARCH = 3;

	/**
	 * 唤醒带数据
	 */
	public static final int WEEK_UP_INTERVAL = 15000;
	/**
	 * 单独唤醒，没有回复
	 */
	public static final int WEEK_UP_SAMPLE_INTERVAL = 8000;
	/**
	 * 数据返回间隔时间
	 */
	public static final int READ_INTERVAL = 8000;

	public static final int READ_INTERVAL_SHORT = 5000;
	/**
	 * NFC设备唤醒持续时间
	 */
	public static final int NFC_T_ALIVE = 100000;

	public static final int MAX_HISTROY_SIZE = 999;

	public static final int DEFAULT_STATE = 1;

	public static final int READ_RETRY_TIMES = 2;


	public static final int Handler_SHOW_RESULT = 1999;
	public static final int Handler_ADD_RESPONSE = 2000;
	public static final int Handler_ADD_ERROR = 2001;


	public static final String WEEKUPPACKET = "weekuppacket";

	public static final String EXTHDMETER = "exthdmeter";

	public static final String HDNFCMETER = "hdnfcmeter";

	public static final int YSZT = 0;

	//获取手机的唯一标识
	public static String getPhoneSign() {
		StringBuilder deviceId = new StringBuilder();
		try {
			//IMEI（imei）
			TelephonyManager tm = (TelephonyManager) AcquistionApp.getInstance().getSystemService(Context.TELEPHONY_SERVICE);
			if (ActivityCompat.checkSelfPermission(AcquistionApp.getInstance(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
				// TODO: Consider calling
				//    ActivityCompat#requestPermissions
				// here to request the missing permissions, and then overriding
				//   public void onRequestPermissionsResult(int requestCode, String[] permissions,
				//                                          int[] grantResults)
				// to handle the case where the user grants the permission. See the documentation
				// for ActivityCompat#requestPermissions for more details.
				return "";
			}
			String imei = tm.getDeviceId();
			if(!TextUtils.isEmpty(imei)){
				deviceId.append(imei);
				return deviceId.toString();
			}
			//序列号（sn）
			String sn = tm.getSimSerialNumber();
			if(!TextUtils.isEmpty(sn)){
				deviceId.append(sn);
				return deviceId.toString();
			}
			//如果上面都没有， 则生成一个id：随机码
			String uuid = getUUID();
			if(!TextUtils.isEmpty(uuid)){
				deviceId.append(uuid);
				return deviceId.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			deviceId.append(getUUID());
		}
		return deviceId.toString();
	}
	/**
	 * 得到全局唯一UUID
	 */
	private static String uuid;
	public static String getUUID(){
		SharedPreferences mShare =  AcquistionApp.getInstance().getSharedPreferences("uuid",MODE_PRIVATE);
		if(mShare != null){
			uuid = mShare.getString("uuid", "");
		}
		if(TextUtils.isEmpty(uuid)){
			uuid = UUID.randomUUID().toString();
			mShare.edit().putString("uuid",uuid).commit();
		}
		return uuid;
	}
}
